package com.xbd.flex.service.impl;

import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryOrderBy;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.xbd.flex.dto.UserDTO;
import com.xbd.flex.entity.User;
import com.xbd.flex.mapper.UserMapper;
import com.xbd.flex.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.mybatisflex.core.query.QueryMethods.noCondition;
import static com.xbd.flex.entity.table.UserTableDef.USER;

@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    public Page<User> listUserPage(UserDTO userDTO) {
        QueryWrapper wrapper = QueryWrapper.create();
        String keyword = userDTO.getKeyword();
        wrapper.where(StrUtil.isNotBlank(keyword) ?
                USER.USERNAME.like(keyword).or(USER.PASSWORD.like(keyword)) :
                noCondition());
        wrapper.orderBy(new QueryOrderBy(USER.CREATE_DATE, "DESC"));
        return page(new Page<>(userDTO.getPage(), userDTO.getPageSize()), wrapper);
    }

    public List<User> listUser() {
        return userMapper.listUser();
    }
}
